package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.service.wx.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author heyyon
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        Session session = SecurityUtils.getSubject().getSession();
        System.out.println("sessionId in addressController "+session.getId());
        Integer userId = (Integer) session.getAttribute("id");
        List<Address> addresses = wxAddressService.list(userId);
        return BaseRespVo.ok(addresses);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        Address address = wxAddressService.detail(id);
        return BaseRespVo.ok(address);
    }

    @PostMapping("save")
    public BaseRespVo save(@RequestBody Address address) {
        Integer id= (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
        int row = wxAddressService.save(address,id);
        if (row != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Address address) {
        int row=wxAddressService.delete(address.getId());
        if (row != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }
}
